package edu.vt.cbil.dataset;

import edu.vt.cbil.util.BioToolbox;

import java.io.*;


/**
 * @author Justin
 * @version 1.0
 * @created 09-11月-2014 11:52:11
 */
public final class ProbeSet implements Comparable<ProbeSet>, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -5568644592317301301L;
    public Long Loci;
    public String ProbeSetID;
    public short Chr_id;
    public int Start;
    //public String Cytoband;
    public boolean Is_CNProbe;
    public boolean Is_SNPProbe;
    public ProbeSetData data;

    public ProbeSet() {
        data = new ProbeSetData();
    }

    public ProbeSet(RawIntensityProbeSet rp) {
        this();
        Loci = rp.Loci;

        Chr_id = rp.Chr_id;
        Start = rp.Start;
        Is_CNProbe = rp.Is_CNProbe;
        Is_SNPProbe = rp.Is_SNPProbe;

//为减少内存使用，下面的数据只有调试时使用
        ProbeSetID = rp.ProbeSetID;
//        Cytoband = rp.Cytoband;

    }

//    public void finalize() throws Throwable {
//        super.finalize();
//        data.Normal = null;
//        data.Tumor = null;
//    }

    @Override
    public int compareTo(ProbeSet o) {
        return Loci.compareTo(o.Loci);
    }

    public void translating(double[] thresh) {
        double min_value = 0.1;
        if (Is_SNPProbe) {
            double an = (data.Normal.mean_A > thresh[0]) ? data.Normal.mean_A - thresh[0] : min_value;
            double bn = (data.Normal.mean_B > thresh[0]) ? data.Normal.mean_B - thresh[0] : min_value;
            double at = (data.Tumor.mean_A > thresh[1]) ? data.Tumor.mean_A - thresh[1] : min_value;
            double bt = (data.Tumor.mean_B > thresh[1]) ? data.Tumor.mean_B - thresh[1] : min_value;
            data.Normal.mean_A = an;
            data.Normal.mean_B = bn;
            data.Tumor.mean_A = at;
            data.Tumor.mean_B = bt;
        }

        if (Is_CNProbe) {
            data.Normal.mean_CN = (data.Normal.mean_CN > thresh[0]) ? data.Normal.mean_CN - thresh[0] : min_value;
            data.Tumor.mean_CN = (data.Tumor.mean_CN > thresh[1]) ? data.Tumor.mean_CN - thresh[1] : min_value;
        }
    }

    public void calculateAlleleSignals() {
        if (Is_SNPProbe) {
            double noramlCN = data.Normal.mean_A + data.Normal.mean_B;
            double a = BioToolbox.divide(data.Tumor.mean_A, noramlCN);
            double b = BioToolbox.divide(data.Tumor.mean_B, noramlCN);
            if (!Double.isNaN(a) && !Double.isNaN(b)) {
                data.alleleA = a;
                data.alleleB = b;

                data.Is_Valid = true;
            } else {
                data.Is_Valid = false;
            }
        } else if (Is_CNProbe) {
            double cn = BioToolbox.divide(data.Tumor.mean_CN, data.Normal.mean_CN);

            if (!Double.isNaN(cn)) {
                data.alleleCN = cn;

                data.Is_Valid = true;
            } else {
                data.Is_Valid = false;
            }
        }
    }

    public void calculateCopyNumber() {

        BufferedWriter out = null;
        if (Is_SNPProbe) {

            data.CopyNumberHalf = data.alleleA + data.alleleB;
            data.LRR = BioToolbox.log2(2 * data.CopyNumberHalf);
            data.BAF = data.alleleB / data.CopyNumberHalf;

        } else if (Is_CNProbe) {
            data.CopyNumberHalf = data.alleleCN;
        }
        data.old_half = data.CopyNumberHalf;
    }

//	public void calculateCopyNumber() {
//
//		if (Is_SNPProbe) {
//			double sumN = data.Normal.mean_A + data.Normal.mean_B;
//			double sumT = data.Tumor.mean_A + data.Tumor.mean_B;
//
//			if ((sumN > 0)&&(sumT > 0)) {
//
//				data.Normal.mean_CN = sumN;
//				data.Tumor.mean_CN = sumT;
//				data.CopyNumberHalf = data.Tumor.mean_CN / sumN;
//
//				data.alleleA = data.Tumor.mean_A / sumN;
//				data.alleleB = data.Tumor.mean_B / sumN;
//
//				data.Is_Valid = true;
//			} else if ((sumN == 0)&&(sumT == 0)){
//				data.Normal.mean_CN = 0;
//				data.Tumor.mean_CN = 0;
//				data.CopyNumberHalf = 1;
//				data.alleleA = 0;
//				data.alleleB = 0;
//
//				data.Is_Valid = true;
//			} else {
//				data.Is_Valid = false;
//			}
//		}
//
//		if (Is_CNProbe) {
//			if ((data.Normal.mean_CN > 0)&&(data.Tumor.mean_CN > 0)) {
//				data.CopyNumberHalf = data.Tumor.mean_CN / data.Normal.mean_CN;
//				data.Is_Valid = true;
//			} else if ((data.Normal.mean_CN == 0)&&(data.Tumor.mean_CN == 0)) {
//				data.CopyNumberHalf = 1;
//				data.Is_Valid = true;
//			}else	{
//				data.Is_Valid = false;
//			}
//		}
//
//	}

//	public void translating(double[] thresh){
//		if (Is_SNPProbe) {
//			data.Normal.mean_A = data.Normal.mean_A - thresh[0];
//			data.Normal.mean_B = data.Normal.mean_B - thresh[0];
//			data.Tumor.mean_A = data.Tumor.mean_A - thresh[1];
//			data.Tumor.mean_B = data.Tumor.mean_B - thresh[1];
//		}
//
//		if (Is_CNProbe) {
//			data.Normal.mean_CN = data.Normal.mean_CN - thresh[0];
//			data.Tumor.mean_CN = data.Tumor.mean_CN - thresh[1];
//		}
//	}

}